<?php
namespace app\api\controller;

use app\common\controller\Api;

use app\api\controller\ela;

//SOAP Client
//non-WSDL 模式
class Testsoapclient extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    //http://ela.test/api/testsoapclient/func0
    public function func0() {
        try {
            $client = new \SoapClient("http://office.ecatch.com.tw:8739/eWMSMessage/mex");
//            var_dump($client->__getFunctions()); // eWMSServiceResponse eWMSService(eWMSService $parameters)
//            var_dump($client->__getTypes());

            $xml = "<eWMSMessage>
	<WebService_ID>ERP</WebService_ID>
	<EventID>SendSKUData</EventID>
	<SKUDataList>
		<SKUDataInfo>
			<SKU>106999537</SKU>
			<SKUName>戰車輪固定端</SKUName>
			<Specification>2610.34PZB.A1</Specification>
			<InventoryUnit>PCS</InventoryUnit>
			<SKUType1>原材料</SKUType1>
			<SKUType2></SKUType2>
			<SKUType3></SKUType3>
			<SKUType4></SKUType4>
			<ProductDescription>商品描述</ProductDescription>
			<StandardSKU>標準途程品號</StandardSKU>
			<StandardCodeNumber>標準途程代號</StandardCodeNumber>
			<MainWarehouseId>6401</MainWarehouseId>
			<InventoryManagement>庫存管理</InventoryManagement>
			<LotManagement>106999537</LotManagement>
			<SKUAttribute>品號屬性</SKUAttribute>
			<Note>備註</Note>
			<EffectiveDateTime>2022/01/12</EffectiveDateTime>
			<FailureDateTime>2025/12/31</FailureDateTime>
			<InspectionMethod>0</InspectionMethod>
		</SKUDataInfo>
	</SKUDataList>
</eWMSMessage>
";
//            $params = array(
//                "clsWMSService" => array(
//                    "XML_Message"=>$XML_Message
//                ),
//                "ReportData" => array(
//                    "RtnCode"=>"",
//                    "RtnReason"=>""
//                ),
//                "eWMSService" => array(
//                    "input"=> array(
//                        "XML_Message"=>$XML_Message
//                    ),
//                ),
//                "eWMSServiceResponse" => array(
//                    "eWMSServiceResult"=> array(
//                        "RtnCode"=>"",
//                        "RtnReason"=>""
//                    ),
//                ),
//                "char" => 11,
//                "duration" => 11,
//                "guid" => "",
//            );

            $params = array(
                "input"=> array(
                    "XML_Message"=>$xml
                ),
            );

            $response = $client->__soapCall("eWMSService", array($params));
        } catch (Exction $e) {
            echo print_r($e->getMessage(), true);
        }

        $this->success('返回成功', $response);

    }

    //http://ela.test/api/testsoapclient/func2
    //这个可以
    public function func2()
    {
        try {
            $soap = new \SoapClient(null, array(
                "location" => "http://ela.test/index/modulestock/info",
                "uri" => "abcd",  //资源描述符服务器和客户端必须对应
                'trace' => true,
                'encoding' => 'utf-8'));
            $s = $soap->info("bad11f");

        } catch (Exction $e) {
            echo print_r($e->getMessage(), true);
        }
        $this->success('返回成功', $s);
    }

    //http://ela.test/api/testsoapclient/func3
    public function func3()
    {
        try {
            $soap = new \SoapClient(null, array(
                "location" => "http://ela.test/index/modulestock/info2",//http://220.132.102.45:8897/index/modulestock/info2
                "uri" => "abcd",  //资源描述符服务器和客户端必须对应
                'trace' => true,
                'encoding' => 'utf-8'));
            $aa = $soap->__soapCall('info2', array("aa" => "dqqwwq"));

        } catch (Exction $e) {
            echo print_r($e->getMessage(), true);
        }
        $this->success('返回成功', $aa);
    }

    //http://ela.test/api/testsoapclient/test
    public function test()
    {
        $this->success('返回成功');
    }

    function obj2array($obj)
    {
        $out = array();
        foreach ($obj as $key => $val) {
            switch (true) {
                case is_object($val):
                    $out[$key] = obj2array($val);
                    break;
                case is_array($val):
                    $out[$key] = obj2array($val);
                    break;
                default:
                    $out[$key] = $val;
            }
        }
        return $out;
    }

    //http://ela.test/api/testsoapclient/testReturn
    //这个可以
    public function testReturn()
    {
        $xml = "<eWMSMessage>
	<WebService_ID>ERP</WebService_ID>
	<EventID>InventoryData</EventID>
	<InventoryDataList>
		<InventoryDataInfo>
			<POType>單別</POType>
			<POId>盤點底稿編號</POId>
			<InventoryDetailDataList>
				<InventoryDetailDataInfo>
					<SerialId>盤點底稿序號</SerialId>
					<SKU>品號</SKU>
					<CarrierID>棧板編號</CarrierID>
					<LotId>專案代碼</LotId>
					<InventoryQty>盤點數量</InventoryQty>
				</InventoryDetailDataInfo>
				<InventoryDetailDataInfo>
					<SerialId>盤點底稿序號</SerialId>
					<SKU>品號</SKU>
					<CarrierID>棧板編號</CarrierID>
					<LotId>專案代碼</LotId>
					<InventoryQty>盤點數量</InventoryQty>
				</InventoryDetailDataInfo>
			</InventoryDetailDataList>
		</InventoryDataInfo>
	</InventoryDataList>
</eWMSMessage>					";

//        $data = $this->xmlToArray($xml);
//        $this->doEla($data);

        //http://ela.test/index/modulestock/info
        $location = "http://".$_SERVER['HTTP_HOST']."/index/modulestock/info";

        try {
            //http://ela.test/index/modulestock/info
            $soap = new \SoapClient(null, array(
//                "location" => "http://220.132.102.45:8897/index/modulestock/info",
                "location" => $location,
                "uri" => "abcd",  //资源描述符服务器和客户端必须对应
                'trace' => true,
                'encoding' => 'utf-8'));
            $s = $soap->returninfo($xml);

        } catch (Exction $e) {
            echo print_r($e->getMessage(), true);
        }

//        echo $location;
        $this->success('返回成功', $s);
    }


    //http://ela.test/api/testsoapclient/testReturn2
    public function testReturn2()
    {
        $xml = "<eWMSMessage>
	<WebService_ID>ERP</WebService_ID>
	<EventID>InventoryData</EventID>
	<InventoryDataList>
		<InventoryDataInfo>
			<POType>單別</POType>
			<POId>盤點底稿編號</POId>
			<InventoryDetailDataList>
				<InventoryDetailDataInfo>
					<SerialId>盤點底稿序號</SerialId>
					<SKU>品號</SKU>
					<CarrierID>棧板編號</CarrierID>
					<LotId>專案代碼</LotId>
					<InventoryQty>盤點數量</InventoryQty>
				</InventoryDetailDataInfo>
				<InventoryDetailDataInfo>
					<SerialId>盤點底稿序號</SerialId>
					<SKU>品號</SKU>
					<CarrierID>棧板編號</CarrierID>
					<LotId>專案代碼</LotId>
					<InventoryQty>盤點數量</InventoryQty>
				</InventoryDetailDataInfo>
			</InventoryDetailDataList>
		</InventoryDataInfo>
	</InventoryDataList>
</eWMSMessage>					";

        try {
            $client = new \SoapClient("http://ela.test/index/modulestock2");
//            $client = new \SoapClient("http://ela.test/ela.wsdl");
            $response = $client->returninfo($xml);
        } catch (Exction $e) {
            echo print_r($e->getMessage(), true);
        }

        $this->success('返回成功', $response);
    }

    //http://ela.test/api/testsoapclient/func6
    public function func6()
    {
        $xml = "<eWMSMessage>
	<WebService_ID>ERP</WebService_ID>
	<EventID>InventoryData</EventID>
	<InventoryDataList>
		<InventoryDataInfo>
			<POType>單別</POType>
			<POId>盤點底稿編號</POId>
			<InventoryDetailDataList>
				<InventoryDetailDataInfo>
					<SerialId>盤點底稿序號</SerialId>
					<SKU>品號</SKU>
					<CarrierID>棧板編號</CarrierID>
					<LotId>專案代碼</LotId>
					<InventoryQty>盤點數量</InventoryQty>
				</InventoryDetailDataInfo>
				<InventoryDetailDataInfo>
					<SerialId>盤點底稿序號</SerialId>
					<SKU>品號</SKU>
					<CarrierID>棧板編號</CarrierID>
					<LotId>專案代碼</LotId>
					<InventoryQty>盤點數量</InventoryQty>
				</InventoryDetailDataInfo>
			</InventoryDetailDataList>
		</InventoryDataInfo>
	</InventoryDataList>
</eWMSMessage>					";

        $client = new \SoapClient("http://220.132.102.45:8897/server.php?wsdl");
        try {
            $result = $client->getxml($xml);
            print "The answer is: $result";
        } catch (SoapFault $e) {
            print "Sorry an error was caught executing your request: {$e->getMessage()}";
        }
    }

    function xmlToArray($xml)
    {
        //禁止引用外部xml实体
        libxml_disable_entity_loader(true);
        $values = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
        return $values;
    }

    function doEla($data)
    {
        if (!isset($data['EventID'])) {
            return;
        }

        if ($data['EventID'] == 'BuyData') {
            //進貨單回報
            foreach ($data['BuyDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'SellData') {
            //銷貨單回報
            foreach ($data['SellDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'ProduceInData') {
            //生產入庫單回報
            foreach ($data['ProduceInDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'PickUpData') {
            //領料單回報
            foreach ($data['PickUpDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'ReturnData') {
            //退料單回報
            foreach ($data['ReturnDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }
        } else if ($data['EventID'] == 'SellBackData') {
            //銷退單回報
            foreach ($data['SellBackDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }
        } else if ($data['EventID'] == 'TransferData') {
            //調撥單回報
            foreach ($data['TransferDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }

        } else if ($data['EventID'] == 'BuyReturnData') {
            //退貨單回報
            foreach ($data['BuyReturnDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }
        } else if ($data['EventID'] == 'InventoryData') {
            //盤點單回報
            foreach ($data['InventoryDataList'] as $row) {
                $main = \app\admin\model\ela\Mainlist::where('receipt', $row['POType'])->where('receipt_number', $row['POId'])->find();
                if ($main) {
                    $main->status = '4';
                    $main->save();
                }
            }
        } else {
            //單據狀態回報格式
            foreach ($data['Data']['FormHead']['RecordList'] as $row) {
                if (isset($row['MO001'])) {

                } else if (isset($row['PU001'])) {
                    //進貨單單別
                    if (intval($row['PU200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['PU001'])->where('receipt_number', $row['PU002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }
                } else if (isset($row['CO001'])) {
                    //銷貨單單別
                    if (intval($row['CO200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['CO001'])->where('receipt_number', $row['CO002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['MP001'])) {
                    //生產入庫單單別
                    if (intval($row['MP200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['MP001'])->where('receipt_number', $row['MP002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }
                } else if (isset($row['MU001'])) {
                    //領料單單別
                    if (intval($row['MU200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['MU001'])->where('receipt_number', $row['MU002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['MR001'])) {
                    //退料單單別
                    if (intval($row['MR200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['MR001'])->where('receipt_number', $row['MR002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['CR001'])) {
                    //銷退單單別
                    if (intval($row['CR200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['CR001'])->where('receipt_number', $row['CR002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['IN001'])) {
                    //調撥單單別
                    if (intval($row['IN200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['IN001'])->where('receipt_number', $row['IN002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['PR001'])) {
                    //退貨單單別
                    if (intval($row['PR200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['PR001'])->where('receipt_number', $row['PR002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                } else if (isset($row['IS001'])) {
                    //盤點單單別
                    if (intval($row['IS200']) == -1) {
                        $main = \app\admin\model\ela\Mainlist::where('receipt', $row['IS001'])->where('receipt_number', $row['IS002'])->find();
                        if ($main) {
                            $main->status = '3';
                            $main->save();
                        }
                    }

                }
            }

        }


    }

}

//SOAP server
///Users/colin/Downloads/Miro/ela/application/index/controller/Modulestock.php